#Python's interprocess communication (IPC)
A process has its own independent memory space and resources. Trying to pass information through global variables doesn't work.
For example, in the code below, the child process prints an empty string instead of Ciallo~(∠・ω< )⌒★:
from multiprocessing import Process
message: str = ''
def worker():
print(message)
if __name__ == '__main__':
message = 'Ciallo~(∠・ω< )⌒★' # Attempting to send data
p = Process(target=worker) # Create process
p.start() # Start process
p.join() # Wait for process to finish
In Python, you can use a Queue or Pipe from the multiprocessing module for interprocess communication (IPC).
#Queue
Create a queue with the Queue class from the multiprocessing module. Use the put method to add data to the tail and the get method to remove data from the head.
Example:
from multiprocessing import Process, Queue
# Producer, sends data
def producer(q: Queue):
for i in range(5):
q.put(i) # Send data
q.put(None) # Send None to signal end
# Consumer, receives data
def consumer(q: Queue):
while (item := q.get()) is not None:
print(f'Received {item}')
if __name__ == '__main__':
q = Queue() # Create queue
p1 = Process(target=producer, args=(q,))
p2 = Process(target=consumer, args=(q,))
p1.start() # Start processes
p2.start()
p1.join() # Wait for processes to finish
p2.join()
Output:
Received 0 Received 1 Received 2 Received 3 Received 4
#Pipe
Use the Pipe function from the multiprocessing module to create a pipe. It returns two connection endpoints—data written on one end can be read from the other.
Example:
from multiprocessing import Process, Pipe
from multiprocessing.connection import PipeConnection
# Producer, sends data
def producer(tx: PipeConnection):
for i in range(5):
tx.send(i) # Send data
tx.send(None) # Send None to signal end
# Consumer, receives data
def consumer(rx: PipeConnection):
while (item := rx.recv()) is not None:
print(f'Received {item}')
if __name__ == '__main__':
tx, rx = Pipe() # Create pipe
p1 = Process(target=producer, args=(tx,))
p2 = Process(target=consumer, args=(rx,))
p1.start() # Start processes
p2.start()
p1.join() # Wait for processes to finish
p2.join()
Output:
Received 0 Received 1 Received 2 Received 3 Received 4